<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Nuhaa All Bakry, Tan Chin Voon, Nazarul Azril
 *
 */

class Protocol extends AppModel {

    var $name = 'Protocol';

/**
 * Defining the name of the table
 *
 */
    var $useTable = 'protocols';
/**
 * Displaying protocol name
 *
 */
    var $displayField ='protocol_title';
    
/**
 * Validating the fields in protocol model
 *
 */
 
     var $validate = array(
            'protocol_title' => array(
                    'required'=>array(
                        'rule' => 'notEmpty'
                    )
                ),
             
            'rank' =>array(
                    'required'=>array(
                        'rule' => 'notEmpty'
                    ),
                    'unique'=>array(
                        'rule' => array('checkUnique', 'rank')
                    )
            ));  


//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
 * Building assosiations betweeen models 
 *
 */
    var $hasMany = array(
            'User' => array('className' => 'User',
                                'foreignKey' => 'protocol_id',
                                'dependent' => false,
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'limit' => '',
                                'offset' => '',
                                'exclusive' => '',
                                'finderQuery' => '',
                                'counterQuery' => ''
            )
    );

    function getProtocolsOnly(){
        $this->unbindModel( array('hasMany' => array('User')) );
        $protocols = $this->find('all',array('order'=>'rank ASC'));
        $ZeroProtocols = $this->find('all',array( 'conditions'=>array('Protocol.id'=>'0')));
        if (!$ZeroProtocols) {
            $new_protocols = array(array( 'Protocol'=>array('id'=>0,'protocol_title'=>'','rank'=>'')));
            $g = array_merge($new_protocols,$protocols);
            $protocols = $g;
        }
        return $protocols;
    }

    function softByProtocol( $memberships ){
        $users = $memberships;
        $new_users = array();
        $protocols = $this->getProtocolsOnly('','','Protocol.rank ASC');
        if ( $protocols ) {
            foreach ( $protocols as $protocol) {
                $user_num = 0;
                foreach ( $users as $user) {
                    if ( $protocol['Protocol']['id'] == $user['User']['protocol_id']) {
                        array_push($new_users,$users[$user_num]);
                    }//end if
                    $user_num++;
                }
            }
        } else {
            $new_users = $memberships;
        }
        return $new_users;
    }


}
?>
